package com.nikanbujian.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nikanbujian.reggie.common.R;
import com.nikanbujian.reggie.dto.DishDto;
import com.nikanbujian.reggie.entity.Category;
import com.nikanbujian.reggie.entity.Dish;
import com.nikanbujian.reggie.entity.DishFlavor;
import com.nikanbujian.reggie.service.ICategoryService;
import com.nikanbujian.reggie.service.IDishFlavorService;
import com.nikanbujian.reggie.service.IDishService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-20
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    IDishService iDishService;
    @Autowired
    IDishFlavorService iDishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    ICategoryService iCategoryService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dto) {
        iDishService.saveDish(dto);
        //只要有更新就清理所有缓存
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        //清理你改的缓存
        String key="dish_"+dto.getCategoryId()+"_"+dto.getStatus();
        redisTemplate.delete(key);
        return R.success("新增成功");
    }

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name){
        Page<Dish> page1=new Page<>(page,pageSize);
        Page<DishDto> page2=new Page<>();
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,Dish::getName,name);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        iDishService.page(page1,lambdaQueryWrapper);
        BeanUtils.copyProperties(page1,page2,"records");
        List<Dish> records = page1.getRecords();
        List<DishDto> dishDtos=records.stream().map((item) ->{
            DishDto dto=new DishDto();
            BeanUtils.copyProperties(item,dto);
            Long categoryId = item.getCategoryId();
            Category category = iCategoryService.getById(categoryId);
            String name1 = category.getName();
            dto.setCategoryName(name1);
            return dto;
        }).collect(Collectors.toList());
        page2.setRecords(dishDtos);

        return R.success(page2);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dto){
        iDishService.updateDish(dto);
        //只要有更新就清理所有缓存
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        //清理你改的缓存
        String key="dish_"+dto.getCategoryId()+"_"+dto.getStatus();
        redisTemplate.delete(key);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
       return R.success(iDishService.getDishDto(id));
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtos=null;
        String key="dish_"+dish.getCategoryId()+"_"+dish.getStatus();
        //从redis获取数据
        dishDtos= (List<DishDto>) redisTemplate.opsForValue().get(key);
        if (dishDtos!=null){
            return R.success(dishDtos);
        }

        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list = iDishService.list(lambdaQueryWrapper);

        dishDtos=list.stream().map((item) ->{
            DishDto dto=new DishDto();
            BeanUtils.copyProperties(item,dto);
            Long categoryId = item.getCategoryId();
            Category category = iCategoryService.getById(categoryId);
            String name1 = category.getName();
            dto.setCategoryName(name1);
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list1 = iDishFlavorService.list(lambdaQueryWrapper1);
            dto.setFlavors(list1);
            return dto;
        }).collect(Collectors.toList());

        //如果缓存没东西，吧从数据库里查到的东西给缓存
        redisTemplate.opsForValue().set(key,dishDtos,60, TimeUnit.MINUTES);
        return R.success(dishDtos);
    }
}
